
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[Rounds_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[Rounds_Insert]

IF OBJECT_ID(N'[dbo].[Rounds_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[Rounds_Update]

IF OBJECT_ID(N'[dbo].[Rounds_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[Rounds_Delete]

IF OBJECT_ID(N'[dbo].[Rounds_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[Rounds_Select]

--endregion

GO


--region [dbo].[Rounds_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Rounds_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdRound int = null,
	@IdSeason int = null,
	@RoundName nvarchar(100) = null,
	@StartDate datetime = null,
	@EndDate datetime = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[Rounds]
					WHERE
					(
						(@IdRound is null OR [Rounds].[IdRound] = @IdRound)
						AND (@IdSeason is null OR [Rounds].[IdSeason] = @IdSeason)
						AND (@RoundName is null OR [Rounds].[RoundName] like @RoundName)
						AND (@StartDate is null OR [Rounds].[StartDate] = @StartDate)
						AND (@EndDate is null OR [Rounds].[EndDate] = @EndDate)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[Rounds].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdRound' and @SortType = 1 THEN [Rounds].[IdRound] END ASC,
						CASE WHEN @SortBy = 'IdRound' and @SortType = 0 THEN [Rounds].[IdRound] END DESC,
						CASE WHEN @SortBy = 'IdSeason' and @SortType = 1 THEN [Rounds].[IdSeason] END ASC,
						CASE WHEN @SortBy = 'IdSeason' and @SortType = 0 THEN [Rounds].[IdSeason] END DESC,
						CASE WHEN @SortBy = 'RoundName' and @SortType = 1 THEN [Rounds].[RoundName] END ASC,
						CASE WHEN @SortBy = 'RoundName' and @SortType = 0 THEN [Rounds].[RoundName] END DESC,
						CASE WHEN @SortBy = 'StartDate' and @SortType = 1 THEN [Rounds].[StartDate] END ASC,
						CASE WHEN @SortBy = 'StartDate' and @SortType = 0 THEN [Rounds].[StartDate] END DESC,
						CASE WHEN @SortBy = 'EndDate' and @SortType = 1 THEN [Rounds].[EndDate] END ASC,
						CASE WHEN @SortBy = 'EndDate' and @SortType = 0 THEN [Rounds].[EndDate] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[Rounds]
			Where 
			(
				(@IdRound is null OR [Rounds].[IdRound] = @IdRound)
				AND (@IdSeason is null OR [Rounds].[IdSeason] = @IdSeason)
				AND (@RoundName is null OR [Rounds].[RoundName] like @RoundName)
				AND (@StartDate is null OR [Rounds].[StartDate] = @StartDate)
				AND (@EndDate is null OR [Rounds].[EndDate] = @EndDate)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[Rounds_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[Rounds_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Rounds_Insert]
	@IdRound int OUTPUT,
	@IdSeason int,
	@RoundName nvarchar(100),
	@StartDate datetime,
	@EndDate datetime

AS


INSERT INTO [dbo].[Rounds] 
(
	[IdSeason],
	[RoundName],
	[StartDate],
	[EndDate]
)
VALUES 
(
	@IdSeason,
	@RoundName,
	@StartDate,
	@EndDate
)

SET @IdRound = SCOPE_IDENTITY()

--end [dbo].[Rounds_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[Rounds_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Rounds_Update]
	@IdRound int,
	@IdSeason int,
	@RoundName nvarchar(100),
	@StartDate datetime,
	@EndDate datetime
AS


UPDATE [dbo].[Rounds] SET
	[IdSeason] = @IdSeason,
	[RoundName] = @RoundName,
	[StartDate] = @StartDate,
	[EndDate] = @EndDate
WHERE
	[IdRound] = @IdRound

--end [dbo].[Rounds_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[Rounds_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Rounds_Delete]
	@IdRound int
AS


DELETE FROM [dbo].[Rounds]
WHERE
(
	[IdRound] = @IdRound
)

--end [dbo].[Rounds_Delete]
--endregion

GO
--=========================================================================================--

